/*----------------------------------------
$Id: ll.as,v 1.7 2005/10/10 03:09:26 mfrumin Exp $

This file is part of VGMap: http://vgmap.eyebeamresearch.org

Distributed under the Gnu Public License, see LICENSE

Copyright Copyright 2005 Michael Frumin
mfrumin@eyebeam.org, http://eyebeam.org
----------------------------------------*/

function GMap_click(map, overlay, point) {
    this.drawGrid(map);
}

function GMap_moveend(map) {
    this.drawGrid(map);
}

function GMap_movestart(map) {
    // nothin to do!
}

function GMap_move(map) {
    this.drawGrid(map);
}

function GMap_zoom(map, oldzoomlevel, newzoomlevel) {
    this.drawGrid(map);
}

function drawGrid(map) {
    // clear the debugging element
    _parent.debugMsg("", 1);

    // print out the current map data, just for debugging
    debugObj(map, "gMap");

    // wipe the grid before redrawing it
    if(this.grid) {
        grid.removeMovieClip();
    }
    if(this.grid == undefined) {
        this.createEmptyMovieClip("grid", 1);
    }
    g = this.grid;
    g.lineStyle(5, 0xff0000, 75 );



    var proj="merc";
    var gMapBoundsProj = _parent.getGMapProjectedBounds(map, proj);

    // necessary calculations because of the overlay offset/border compared to the map
    var mapImgBounds = _parent.getGMapImgBounds();

    // here we need to figure out how to scale/translate coordinates onto the currently
    // display GMap

    var xTrans = _parent.solve2D({to: mapImgBounds.minX, from: gMapBoundsProj.minX},
            {to: mapImgBounds.maxX, from: gMapBoundsProj.maxX}
                                        );
    // switch min/max Y because of flash reverses the Y direction as compared to lat/lon
    var yTrans = _parent.solve2D({to: mapImgBounds.minY, from: gMapBoundsProj.maxY},
            {to: mapImgBounds.maxY, from: gMapBoundsProj.minY}
                                        );


    proj = _parent.createProjector(proj);

// the grid-step for each GMap zoom level.  totally hand-calibrated
    var zooms = [.001 , .001 , .01 , .01 , .01 , .01 , .1 , .1 , .1 , 1 , 1 , 1 , 10 , 10 , 10 , 10 , 10 , 10];

    // know the current bounds so we know what grid lines to draw
    var gMapBounds = map.boundsLatLng; 


    // draw the horizontal grid lines

    var dY = zooms[map.zoomLevel];
    var yDec = dY;
    var y = Math.floor(gMapBounds.minY / yDec) * yDec;
    var prevLab = null;
    var i = 0;
    for(; y < gMapBounds.maxY; y += dY) {
        
        // find the coordinates of the grid line's endpoints in the desired
        // geographic projection.
        var p1 = proj.GPSToMapCoords(y , gMapBounds.minX);
        var p2 = proj.GPSToMapCoords(y , gMapBounds.maxX );

        // scale/translate them onto the current GMap
        p1 = { x: xTrans.func(p1.x), y: yTrans.func(p1.y) };
        p2 = { x: xTrans.func(p2.x), y: yTrans.func(p2.y) };

        // draw the line
        g.moveto(p1.x, p1.y);
        g.lineto(p2.x, p2.y);



        // label the line

        var lab = this.newLabel(g, 0, p1.y, Math.floor(y / yDec) * yDec);
        lab._y -= lab._height / 2.0;
        lab._x += lab._width / 2.0;

        if(prevLab == null || lab._y > prevLab._y + prevLab._height) {
            prevLab = lab;
        }
        else {
            //lab.removeTextField();
        }
    }


    // draw the vertical grid lines

    var dX = zooms[map.zoomLevel];
    var xDec = dX;
    var x = Math.floor(gMapBounds.minX / xDec) * xDec;
    var i = 0;
    var prevLab = null;
    for(; x < gMapBounds.maxX; x += dX) {
        
        // same process as above
        var p1 = proj.GPSToMapCoords(gMapBounds.minY, x);
        var p2 = proj.GPSToMapCoords(gMapBounds.maxY, x );
        p1 = { x: xTrans.func(p1.x), y: yTrans.func(p1.y) };
        p2 = { x: xTrans.func(p2.x), y: yTrans.func(p2.y) };
        g.moveto(p1.x, p1.y);
        g.lineto(p2.x, p2.y);


        // label the line

        var lab = this.newLabel(g, p1.x, 0, label.text = Math.floor(x / xDec) * xDec);
        if(prevLab == null || lab._x > prevLab._x + prevLab._width) {
            prevLab = lab;
        }
        else {
            lab.removeTextField();
        }
    }

    _root._alpha = 50;
}

function newLabel(mc, x, y, text) {
    mc.createTextField("label" + this.nText, this.nText, x, y, 1, 1);
    var label = mc["label" + this.nText];
    label.autoSize = "center";
    
    label.background = true;
    label.backgroundColor = 0x000000;
    label.html = true;
    label.htmlText = "<b><font size=\"32\" color=\"#00ff00\">" + text + "</font></b>";
    
    this.nText++;

    return label;

}

function debugObj(o, name) {
    _parent.debugMsg("<p> " + name + ":" + _parent.traceObject(o) + "</p>");
}



// just to be safe, wait until the VGMap Flash component actually has the map data

this.onEnterFrame = function() {
    var map = _parent.getCurrentMap();
    if(map != null) {
        this.drawGrid(map);
        this.onEnterFrame = null;
    }
}


